package com.lcc.generate.util;


import com.alibaba.druid.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MyField {

    private static final String numberStr = "int,int2,int4,int8,integer,float,double,smallint,tinyint,bigint,decimal,numeric,bigserial,";
    private static final String stringStr = "varchar,char,bpchar,text,blob,longtext,";
    private static final String boolStr = "bool,";
    private static final String timeStr = "timestamp,date,datetime,";

    public String name;
    public String searchName;
    public String column;
    public String type;
    public String classType;
    public String remark;
    /**
     * 数据库字段备注添加[search:like]
     * like     全模糊查询
     * likeL    左模糊查询
     * list     in查询
     * time     时间范围查询
     */
    public List<String> searchType;
    /**
     * 数据库字段备注添加[form:radio]
     * 表单类型 默认 text 输入框
     * radio 单选框
     *
     */
    public String formType;
    /**
     * 字典code
     */
    public String dictCode;
    public boolean isNumber;
    public boolean isString;
    public boolean isTime;
    public boolean isBool;
    public boolean isSkip;
    public boolean isOrder;
    public Boolean required;
    public String defaultValue;
    public String label;

    public int length;
    public int digits;
    public boolean isHasDefaultValue;

    public MyField(String name, String column, String type, String classType, String remarks, Boolean required, String defaultValue, int length, int digits, boolean isHasDefaultValue){
        this.name = name;
        this.searchName = name;
        this.column = column;
        this.type =type.toLowerCase();
        type = type.toLowerCase()+",";
        this.isNumber = numberStr.contains(type);
        this.isString = stringStr.contains(type);
        this.isTime = timeStr.contains(type);
        this.isBool = boolStr.contains(type);
        this.classType = classType;
        this.required = required;
        this.defaultValue = defaultValue;
        this.length = length;
        this.digits = digits;
        this.isHasDefaultValue = isHasDefaultValue;
        this.searchType = new ArrayList<>();
        if(remarks != null && remarks.contains("[")){
            // 性别 [dict:sex] [search:list] [form:radio] [skip]
            String[] split = remarks.split("\\[");
            this.remark = split[0];
            for (int i = 1; i < split.length; i++) {
                String s = split[i];
                if(s.contains("dict:")){
                    int i1 = s.indexOf("dict:");
                    int i2 = s.indexOf("]", i1);
                    dictCode = s.substring(5, i2);
                }else if(s.contains("search:")){
                    String searchStr = s.substring(7, s.length()-1);
                    searchType.add(searchStr);
                    if(searchStr.startsWith("like")){
                        searchName = name+JDBCUtil.toHump(searchStr, true);
                    }else if(searchType.contains("list")){
                        searchName = name + "InList";
                    }else if(searchType.contains("list-radio")){
                        searchName = name;
                    }
                }else if(s.contains("form:")){
                    formType = s.substring(5, s.length()-1);
                }else if(s.contains("skip")){
                    isSkip = true;
                }else if(s.contains("order")){
                    isOrder = true;
                }else if(s.contains("list-radio]")){
                    searchType.add("list-radio");
                    searchName = name;
                }else if(s.contains("time]")){
                    searchType.add("time");
                    searchName = name+"Time";
                }else if(s.contains("list]")){
                    searchType.add("list");
                    searchName = name + "InList";
                }else if(s.contains("like]")){
                    searchType.add("like");
                    searchName = name+"Like";
                }else if(s.contains("radio]")){
                    formType = "radio";
                }else if(s.contains("textarea]")){
                    formType = "textarea";
                }else if(s.contains("switch]")){
                    formType = "switch";
                }
            }
        }else{
            this.remark = remarks;
        }

        this.label = getFieldLabelValue();

    }

    private String getFieldLabelValue() {
        if (!StringUtils.isEmpty(this.remark)) {
            for (String s : this.remark.trim().split("([ \\[(（{])")) {
                return s;
            }
        }
        return this.remark;
    }
}
